<template>
  <div id="app">
    <!-- <router-view v-if="show" /> -->
    <router-view />
  </div>
</template>

<script>
import axios from 'axios'
// import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
import { mapMutations } from 'vuex'
import datas from './data'
import datas2 from './data copy'
export default {
  data () {
    return {
      res: '',
      show: false,
      isWho: 0
    }
  },
  watch: {
    // res: {
    //   deep: true,
    //   immediate: true,
    //   handler (val) {
    //     console.log('数据改变了', val)
    //   }
    // }
    isWho: {
      immediate: true,
      handler (val) {
        if (val === 0) {
          this.changeinitData(datas)
        } else {
          this.changeinitData(datas2)
        }
      }
    }
  },
  computed: {},
  created () {
    this.getInitData()
    // this.initWebSocket()
  },
  mounted () {
    setInterval(() => {
      if (this.isWho === 0) {
        this.isWho = 1
      } else {
        this.isWho = 0
      }
    }, 5000)
  },
  methods: {
    ...mapMutations(['changeinitData']),
    getInitData () {
      axios({ method: 'post', url: '/osb/data/init' })
        .then(res => {
          if (res.status === 200) {
            console.log(res.data)
            this.res = res.data
            // this.changeinitData(res.data)
            this.changeinitData(datas)
          }
        })
        .catch(err => {
          console.log(err)
        })
      // this.changeinitData(datas)
      // this.show = true
    },
    initWebSocket () {
      // console.log('创建WebSocket')
      this.websock = new WebSocket('ws://123.56.136.158:28083/websocket')
      // this.websock = new WebSocket('ws://localhost:5000')
      this.websock.onmessage = this.websocketonmessage
      this.websock.onerror = this.websocketonerror
      this.websock.onopen = this.websocketonopen
      // this.websock.onclose = this.websocketclose
    },
    // 连接建立之后执行send方法发送数据
    websocketonopen () {
      console.log('开启连接')
      let data = {
        // code: 0,
        // msg: '这是client：初次连接'
      }
      this.websocketsend(data)
    },
    websocketonerror () {
      console.log('WebSocket连接失败')
    },
    // 数据接收
    websocketonmessage (e) {
      console.log('数据接收' + e.data)
      this.res = JSON.parse(e.data)
      this.changeinitData(JSON.parse(e.data))
    },
    // 数据发送
    websocketsend (Data) {
      this.websock.send(Data)
    },
    // 关闭
    websocketclose (e) {
      console.log('已关闭连接', e)
    }
  }
}
</script>

<style lang="less">
html,
body,
#app {
  // width: 7000px;
  // height: 2100px;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
</style>
